package com.zx.kitchen.index.common.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.kitchen.index.common.adapter.BannerAdapter;
import com.zx.kitchen.index.common.adapter.IndexAdapter;
import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.BannerBean;
import com.zx.kitchen.index.common.entity.IndexPage;
import com.zx.kitchen.index.common.view.DividerGridItemDecoration;
import com.zx.kitchen.index.common.view.viewpager.AutoViewPager;
import com.zx.kitchen.index.common.view.viewpager.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

public class IndexActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView mRecyclerView;
    private List<IndexPage> mList = new ArrayList<>();
    private IndexAdapter indexAdapter;
    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private TextView toolbarRight;

    private AutoViewPager indexViewPager;
    private CirclePageIndicator indexIndciator;
    private BannerAdapter mBannerAdapter;
    private List<BannerBean> bannerBeanList = new ArrayList<>();
    private LinearLayout llIndex;
    private LinearLayout llNewBusiness;
    private LinearLayout llIndexMine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        initView();
        initListener();
        initData();

    }

    private void setViewPager() {
        WindowManager wm = this.getWindowManager();
        // 轮播图小圆点
//        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) indexViewPager.getLayoutParams();
//        params.width = wm.getDefaultDisplay().getWidth();
//        params.height = wm.getDefaultDisplay().getHeight() / 4 * 1;
//        indexViewPager.setLayoutParams(params);
        mBannerAdapter = new BannerAdapter(new ArrayList<BannerBean>(), getBaseContext());
        indexViewPager.setAdapter(mBannerAdapter);
        indexIndciator.setViewPager(indexViewPager);
        indexViewPager.startCarousel();// 设置自动轮播

        final float density = getResources().getDisplayMetrics().density;
        indexIndciator.setBackgroundColor(0x00000000);
        indexIndciator.setRadius(4 * density);
        indexIndciator.setPageColor(getResources().getColor(R.color.white));
        indexIndciator.setFillColor(getResources().getColor(R.color.gray));
        indexIndciator.setStrokeColor(getResources().getColor(R.color.gray));
        indexIndciator.setStrokeWidth(1);
    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        toolbarRight = (TextView) view.findViewById(R.id.toolbar_rightLin);

        View headView = findViewById(R.id.auto_pic);
        indexViewPager = (AutoViewPager) headView.findViewById(R.id.index_viewPager);
        indexIndciator = (CirclePageIndicator) headView.findViewById(R.id.index_indicator);

        mRecyclerView = (RecyclerView) findViewById(R.id.index_recyclerview);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        indexAdapter = new IndexAdapter(mList, getBaseContext());
        mRecyclerView.setAdapter(indexAdapter);
        toolbarRight.setText("切换账号");

        llIndex = (LinearLayout) findViewById(R.id.ll_index);
        llNewBusiness = (LinearLayout) findViewById(R.id.ll_new_business);
        llIndexMine = (LinearLayout) findViewById(R.id.ll_index_mine);

        setViewPager();
    }

    private void initData() {

        BannerBean bannerBean1 = new BannerBean();
        BannerBean bannerBean2 = new BannerBean();
        BannerBean bannerBean3 = new BannerBean();
        bannerBean1.setUrl("http://a3.qpic.cn/psb?/V13htDye2CpJd0/PyfoF4woOItyh*3qEujMYr5VNhgCyQQjeSk4F9dYDR8!/b/dH4BAAAAAAAA&ek=1&kp=1&pt=0&bo=wQOAAsEDgAIFACM!&tm=1489114800&sce=0-12-12&rf=viewer_311");
        bannerBean2.setUrl("http://a3.qpic.cn/psb?/V13htDye2CpJd0/73UqaIhYqGft9WOfCn3H5NokOgX2X5bl1YopdWycNME!/b/dG4BAAAAAAAA&ek=1&kp=1&pt=0&bo=wQOAAkAGKgQFCAM!&tm=1489114800&sce=50-1-1&rf=viewer_311");
        bannerBean3.setUrl("http://a3.qpic.cn/psb?/V13htDye2CpJd0/9YPwL7LCSEAQsw5ZpyqNayEsrOFOM90ggGZsfdOIXR0!/b/dG4BAAAAAAAA&ek=1&kp=1&pt=0&bo=gAI6AYACOgEDCC0!&tm=1489114800&sce=50-1-1&rf=viewer_311");
        bannerBeanList.add(bannerBean1);
        bannerBeanList.add(bannerBean2);
        bannerBeanList.add(bannerBean3);
        mBannerAdapter.updateData(bannerBeanList);

        IndexPage indexPage1 = new IndexPage(getResources().getString(R.string.businessCenter), R.mipmap.icon_index1);
        mList.add(indexPage1);
        IndexPage indexPage2 = new
                IndexPage(getResources().getString(R.string.baseInfoSetting), R.mipmap.icon_index2);
        mList.add(indexPage2);
        IndexPage indexPage3 = new
                IndexPage("报表信息 ", R.mipmap.icon_index3);
        mList.add(indexPage3);
        IndexPage indexPage4 = new
                IndexPage("商品报表", R.mipmap.icon_index4);
        mList.add(indexPage4);
        IndexPage indexPage5 = new
                IndexPage("菜品管理 ", R.mipmap.icon_index5);
        mList.add(indexPage5);
        IndexPage indexPage6 = new
                IndexPage("报价中心 ", R.mipmap.icon_index6);
        mList.add(indexPage6);
    }

    private void initListener() {
        indexAdapter.setOnItemClickListener(new IndexAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(IndexActivity.this, BusinessCenterActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(IndexActivity.this, BusinessInfoActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(IndexActivity.this, BusinessReportActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(IndexActivity.this, GoodsReportActivity.class));
                        break;
                }


            }
        });
        toolbarLeft.setOnClickListener(this);
        toolbarRight.setOnClickListener(this);
        mBannerAdapter.setOnButtonItemClick(new BannerAdapter.OnButtonItemClickListener() {
            @Override
            public void onButtonItemClick(View v, int position) {
                Toast.makeText(IndexActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
            }
        });

        llIndex.setOnClickListener(this);
        llIndexMine.setOnClickListener(this);
        llNewBusiness.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftLin:
                finish();
                break;
            case R.id.toolbar_rightLin:
                Toast.makeText(this, "切换账号", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_index:

                break;

            case R.id.ll_index_mine:
                break;
            case R.id.ll_new_business:
                startActivity(new Intent(this, NewBusinessInfoActivity.class));
                break;
        }
    }
}
